$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/shelvesstock/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '商品图片', name: 'productimg', index: 'productimg', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.productimg!=null&&rowdata.productimg!=''){
                        return '<img src='+rowdata.productimg+' width="50" height="50"/>';
                    }else{
                        return '';
                    }

                }  },
			{ label: '商品名称', name: 'productname', index: 'productname', width: 80 },
			{ label: '待上架数量', name: 'onshelves', index: 'onshelves', width: 80},
            { label: '当前库存数量', name: 'stock', index: 'stock', width: 80 },
            { label: '平均成本', name: 'avgcost', index: 'avgcost', width: 80 },
            { label: '总成本', name: 'totalcost', index: 'totalcost', width: 80 },
			{ label: '销售价', name: 'sellprice', index: 'sellprice', width: 80 },
			{ label: '近30天销量', name: 'sellnum', index: 'sellnum', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.getDetails("+rowdata.id+")'>" +
                        "库存分布</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.Statistics(null,null,null);
    vm.queryGroup();
    vm.queryGoodshelves();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            sheelvesgroupid:null,
            sheelvesid:null,
            name:null

		},
		showList: true,
        showInfoList :true,
		title: null,
        shelvesstock: {},
        product:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showInfoList = false;
			vm.shelvesstock = {};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
        getDetails:function (id) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/product/info/"+id,
                success: function(r){
                    vm.product = r.product;
                    $("#productimg").attr("src",r.product.productimg);
                    $("#productname").text(r.product.productname);
                    $("#barcode").text(r.product.barcode);
                    $("#costprice").text(r.product.costprice);
                    $("#sellprice").text(r.product.sellprice);
                    $("#stockwarncount").val(vm.product.stockwarncount==''||vm.product.stockwarncount==null?r.product.lowerlimit:vm.product.stockwarncount);
                    var stockwarning = r.product.stockwarning;
                    $("input:radio[name='type']").eq(stockwarning).attr("checked",'checked');

                }
            });

            $("#infostock").html("");
            var temp = "";
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/productshelvesstcok/infostock/"+id,
                success: function(r){
                    for(var i=0;i<r.productshelvesstcok.length;i++){
                        temp +="<tr><td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>"+r.productshelvesstcok[i].shelvesname+"</td>"+
                        "<td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>"+r.productshelvesstcok[i].stock;
                        var kcyj = $("#stockwarncount").val();
                        if(r.productshelvesstcok[i].stock<kcyj){
                           temp += "<div style='width: 40px;line-height: 16px;background-color: #F07065;font-size: 10px;color: #fff;margin: 0 auto;border-radius: 2px;height: 16px;'>低库存</div>";
                        }
                        temp +="</td><td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>￥"+r.productshelvesstcok[i].sellprice+"</td></tr>";
                    }
                }
            });
            $("#infostock").html(temp);
            vm.showList = false;
            vm.showInfoList = false;


        },
        Statistics: function (name,sheelvesid,sheelvesgroupid) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/shelvesstock/infoStatistics",
                data:{'name': name,"shelvesid": sheelvesid,"group" :sheelvesgroupid},
                success: function(r){
                    $("#kczsl").text(r.shelvesstock.stocktotalcount);
                    $("#kczcb").text(r.shelvesstock.stocktotalprice==null?"￥"+0:"￥"+r.shelvesstock.stocktotalprice);

                }
            });
        },
		saveOrUpdate: function (event) {
            vm.product.stockwarncount = $("#stockwarncount").val();
            vm.product.stockwarning = $("input:radio[name='type']:checked").val();
			$.ajax({
				type: "POST",
			    url: baseURL + "sys/product/update",
                contentType: "application/json",
			    data: JSON.stringify(vm.product),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
						    if(vm.product.stockwarning == 0){
                                $("#infostock").html("");
                                var temp = "";
                                $.ajax({
                                    type: "POST",
                                    async:false,
                                    url: baseURL + "sys/productshelvesstcok/infostock/"+vm.product.id,
                                    success: function(r){
                                        for(var i=0;i<r.productshelvesstcok.length;i++){
                                            temp +="<tr><td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>"+r.productshelvesstcok[i].shelvesname+"</td>"+
                                                "<td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>"+r.productshelvesstcok[i].stock;
                                            var kcyj = $("#stockwarncount").val();
                                            if(r.productshelvesstcok[i].stock<kcyj){
                                                temp += "<div style='width: 40px;line-height: 16px;background-color: #F07065;font-size: 10px;color: #fff;margin: 0 auto;border-radius: 2px;height: 16px;'>低库存</div>";
                                            }
                                            temp +="</td><td style='border-left: 0;padding: 10px;border: 1px solid #ddd;text-align: center;background-color: #fff!important;'>￥"+r.productshelvesstcok[i].sellprice+"</td></tr>";
                                        }
                                    }
                                });
                                $("#infostock").html(temp);
                            }

						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/lowershelves/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryGroup: function(){
            $("#sheelvesgroup_id").html("");
            $.get(baseURL + "sys/shelvesgroup/alllist", function(r){
                var temp = "<option value=''></option><option value='0'>未分组</option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].groupname+"</option>";

                });
                $("#sheelvesgroup_id").append(temp);

            });
        },
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		getInfo: function(id){
			$.get(baseURL + "sys/lowershelves/info/"+id, function(r){
                vm.lowershelves = r.lowershelves;
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showInfoList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name,"shelvesid": vm.q.sheelvesid,"group" :vm.q.sheelvesgroupid},
                page:page
            }).trigger("reloadGrid");
			vm.Statistics(vm.q.name,vm.q.sheelvesid,vm.q.sheelvesgroupid);
		}
	}
});